package com.example.container;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;

import java.util.concurrent.*;

@Configuration
@ComponentScan(basePackages = "com.example")
public class thread {

    @Bean(name = "customThreadPool")
    public ExecutorService taskExecutor() {
        ThreadFactory threadFactory = r -> {
            Thread t = new Thread(r, "MyTimerThread-" + System.currentTimeMillis());
            t.setDaemon(false);
            return t;
        };

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                4,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10),
                threadFactory,
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        executor.allowCoreThreadTimeOut(false);
        return executor;
    }
}